//
//  PKHomeServices.swift
//  Panku
//
//  Created by mac on 2017/2/8.
//  Copyright © 2017年 Monster. All rights reserved.
//

import UIKit
import ReactiveSwift
import ReactiveCocoa
import Result
import MJExtension
import Alamofire

class PKHomeServices: NSObject {
    
    //获取直播列表
    class func getLivesList(del:Int = 0) -> SignalProducer<[PKLiveModel],NSError> {
        
        let url = PKUrl.LIVES_URL + "/search"
        
        let parameters : [String : Any] = ["del":del,];
        let headers: HTTPHeaders = ["Content-Type": "application/json"]
        let networkUtilsFactor = PKNetworkUtilsHelper.factory()
        let networkUtils = networkUtilsFactor()
        return networkUtils.request(url: url, method: .post, parameters: parameters, headers: headers).map({ (json) -> [PKLiveModel] in
            print(json)
            let dict = json as! Dictionary<String, Any>
            let array = dict["entityList"]
            return PKLiveModel.mj_objectArray(withKeyValuesArray: array) as NSArray as! [PKLiveModel]
        })
        
    }
    
    //获取推文列表
    class func getTweetsList(del:Int = 0) -> SignalProducer<[PKTweetModel],NSError> {
        
        let url = PKUrl.TWEETS_URL + "/search"
        
        let parameters : [String : Any] = ["del":del]
        
        let networkUtilsFactor = PKNetworkUtilsHelper.factory()
        let networkUtils = networkUtilsFactor()
        let headers: HTTPHeaders = ["Content-Type": "application/json"]
        return networkUtils.request(url: url, method: .post, parameters: parameters, headers: headers).map({ (json) -> [PKTweetModel] in
            let dict = json as! Dictionary<String, Any>
            let array = dict["entityList"]
            return PKTweetModel.mj_objectArray(withKeyValuesArray: array) as NSArray as! [PKTweetModel]
        })
        
    }
    
    //推文点赞
    class func voteTweet(id:Int) -> SignalProducer<Any,NSError> {
        
        let url = PKUrl.TWEETS_URL + "/" + "\(id)" + "/vote"
        
        let networkUtilsFactor = PKNetworkUtilsHelper.factory()
        let networkUtils = networkUtilsFactor()
        return networkUtils.request(url: url, method: .put, parameters: nil, headers: nil)
    }
    
    //推文分享
    class func shareTweet(id:Int) -> SignalProducer<Any,NSError> {
        
        let url = PKUrl.TWEETS_URL + "/" + "\(id)" + "/share"
        
        let networkUtilsFactor = PKNetworkUtilsHelper.factory()
        let networkUtils = networkUtilsFactor()
        return networkUtils.request(url: url, method: .put, parameters: nil, headers: nil)
    }
    
    //短视频点赞
    class func voteVideo(id:Int) -> SignalProducer<Any,NSError> {
        
        let url = PKUrl.VIDEOS_URL + "/" + "\(id)" + "/vote"
        
        let networkUtilsFactor = PKNetworkUtilsHelper.factory()
        let networkUtils = networkUtilsFactor()
        return networkUtils.request(url: url, method: .put, parameters: nil, headers: nil)
    }
    
    //短视频分享
    class func shareVideo(id:Int) -> SignalProducer<Any,NSError> {
        
        let url = PKUrl.TWEETS_URL + "/" + "\(id)" + "/share"
        
        let networkUtilsFactor = PKNetworkUtilsHelper.factory()
        let networkUtils = networkUtilsFactor()
        return networkUtils.request(url: url, method: .put, parameters: nil, headers: nil)
    }
    
    //获取短视频列表
    class func getVideosList(queryTime:String,offset:Int = 0,max:Int = 10,del:Int = 0) -> SignalProducer<[PKTweetModel],NSError> {
        
        let url = PKUrl.TWEETS_URL + "/search"
        
        let parameters : [String : Any] = ["offset":offset,
                                           "max":max,
                                           "del":del,
                                           "queryTime":queryTime];
        
        let networkUtilsFactor = PKNetworkUtilsHelper.factory()
        let networkUtils = networkUtilsFactor()
        return networkUtils.request(url: url, method: .post, parameters: parameters, headers: nil).map({ (json) -> [PKTweetModel] in
            
            return PKPublishShortVideoViewModel.mj_objectArray(withKeyValuesArray: json) as NSArray as! [PKTweetModel]
        })
        
    }



    
    
}
